﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace System.Utility.Messaging
{
    public class MessageUtility
    {
        public const string KeyValueSepretor = "|";
        public const string KeySepretor = "=";

        public const string UnknownErrorCode = "EUNK99";
        public const string ApplicationErrorPrefix = "AE-";
        public const string SystemErrorPrefix = "SE-";


        public static string LogAndGetMessageCode(Exception ex)
        {
            Logger.LogException(ex.Message, ex, ExceptionLevel.ERROR, LogMessageType.WebMessage);
            Logger.LogMessage(ex.Message, LogLevel.FATAL, LogMessageType.WebMessage);

            //log the exceptiojn here
            string messageCode = UnknownErrorCode;
            if (ex.Message.Contains(ApplicationErrorPrefix))
                messageCode = ex.Message.Replace(ApplicationErrorPrefix, string.Empty).Trim();
            else if (ex.Message.Contains(SystemErrorPrefix))
                messageCode = ex.Message.Replace(SystemErrorPrefix, string.Empty).Trim();


            if (string.IsNullOrEmpty(messageCode))
                messageCode = UnknownErrorCode;

            Logger.LogMessage("Return message code; -" + messageCode, LogLevel.FATAL, LogMessageType.WebMessage);

            return messageCode;

        }


        public static void LogIncommingMessage(MethodBase methodBase, string[] paramsData)
        {
            try
            {
                string message = methodBase.Name + " web service method has been called at - " + DateTime.Now.ToString() + " and passed values are ";
                foreach (string str in paramsData)
                {
                    if (string.IsNullOrEmpty(str))
                        message += "null,";
                    else
                        message += str + ",";
                }

                Logger.LogMessage(message, LogLevel.FATAL, LogMessageType.WebMessage);
            }
            finally
            { }
        }


    }
}
